/**
	admin模块js函数库
*/

// 注意js中正则表达式需要用一对正斜线包围
function checkName(name) {
    //checkTeacherNameRepeat(name);
	var match = /\s/;
	if (name.length > 30 || name.length < 1) {
		$("#nameSuccess").removeClass("show");
        $("#nameSuccess").addClass("hidden");
        $("#nameDanger").removeClass('hidden');
        $("#nameDanger").addClass('show');
		return false;
	} else {
		if (match.test(name)) {
            $("#nameSuccess").removeClass("show");
            $("#nameSuccess").addClass("hidden");
			$("#nameDanger").removeClass('hidden');
            $("#nameDanger").addClass('show');
			return false;
		}else {
            $("#nameDanger").removeClass('show');
            $("#nameDanger").addClass('hidden');
            $("#nameSuccess").removeClass("hidden");
            $("#nameSuccess").addClass("show");
			return true;
		}
	}
}

function checkTeacherNameRepeat(name) {
	let flag = true;
	$.ajax({
		type: 'POST',
		url: 'checkNameRepeat',
		dataType: 'json',
        async: false,
		data: {
			name : name,
		},
		success: function(data) {
			data = JSON.parse(data);
			if (data.result == "true") {
				flag = false;
                $("#nameRepeat").removeClass("hidden");
                $("#nameRepeat").addClass("show");
			}else {
				flag = true;
                $("#nameRepeat").removeClass("show");
                $("#nameRepeat").addClass("hidden");
			}
		},
		error: function(error) {
			flag = false;
		}
	});
	return flag;
}

function checkStudentNameRepeat(name) {
    let flag = true;
    $.ajax({
        type: 'POST',
        url: 'checkNameRepeat',
        dataType: 'json',
        async: false,
        data: {
            name : name,
        },
        success: function(data) {
            data = JSON.parse(data);
            if (data.result == "true") {
                flag = false;
                $("#nameRepeat").removeClass("hidden");
                $("#nameRepeat").addClass("show");
            }else {
                flag = true;
                $("#nameRepeat").removeClass("show");
                $("#nameRepeat").addClass("hidden");
            }
        },
        error: function(error) {
            flag = false;
        }
    });
    return flag;
}

function checkPassword(password) {
	if (password.length < 8 || password.length > 25) {
        $("#passwordSuccess").removeClass("show");
        $("#passwordSuccess").addClass("hidden");
        $("#passwordDanger").removeClass('hidden');
        $("#passwordDanger").addClass('show');
		return false;
	}else {
		if (password.search(/[a-zA-Z]/) == -1 || password.search(/[0-1]/) == -1 || password.search(/[!@#$%^&*]/) == -1) {
            $("#passwordSuccess").removeClass("show");
            $("#passwordSuccess").addClass("hidden");
            $("#passwordDanger").removeClass('hidden');
            $("#passwordDanger").addClass('show');
			return false;
		}else {
            $("#passwordDanger").removeClass('show');
            $("#passwordDanger").addClass('hidden');
            $("#passwordSuccess").removeClass("hidden");
            $("#passwordSuccess").addClass("show");
			return true;
		}
	}
}

function checkTeacher() {
	var teacherName = $("#teacherName").val();
	var teacherPassword = $("#teacherPassword").val();
	if (! checkTeacherNameRepeat(teacherName)) {
		return false;
	}
	if (!checkName(teacherName)) {
		$("#nameError").removeClass("hidden").addClass("show");
	}else {
        $("#nameError").removeClass("show").addClass("hidden");
	}
	if (!checkPassword(teacherPassword)) {
		$("#passwordError").removeClass("hidden").addClass("show");
	}else {
        $("#passwordError").removeClass("show").addClass("hidden");
	}
	return checkName(teacherName) && checkPassword(teacherPassword);
}

function changeValue(obj) {
	if (obj.checked) {
		obj.value = obj.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.textContent;
	}else {
		obj.value = "";
	}
}

function checkAllOrNot(obj) {
	let check = obj.checked;
	let checkboxes = $("input[name='items[]']");
	for (let i in checkboxes) {
		checkboxes[i].checked = check;
	}
}

function checkStudent() {
	let studentName = $("#studentName").val();
	let studentPassword = $("#studentPassword").val();
	if (!checkStudentNameRepeat(studentName)) {
		return false;
	}
    if (!checkName(studentName)) {
        $("#nameError").removeClass("hidden").addClass("show");
    }else {
        $("#nameError").removeClass("show").addClass("hidden");
    }
    if (!checkPassword(studentPassword)) {
        $("#passwordError").removeClass("hidden").addClass("show");
    }else {
        $("#passwordError").removeClass("show").addClass("hidden");
    }
    return checkName(teacherName) && checkPassword(teacherPassword);
}